<template>
	<view class="chatInterface">
		<view class="scroll-view">
			<view class="all-history-loaded" v-if="!messages.length">
				暂无聊天记录
			</view>
			<!--已经收到的消息-->
			<view v-for="(message,index) in messages" :key="index">
				<!--时间显示，类似于微信，隔5分钟不发言，才显示时间-->
				<view class="time-lag">
					{{renderMessageDate(message, index)}}
				</view>
				<view class="message-item" :class="{'self' : message.Cid == $userInfo.ID}">
					<view class="avatar">
						<image :src="message.Top"></image>
					</view>
					<!-- <view class="avatar" v-else>
						<image :src="currentUser.avatar"></image>
					</view> -->
					<view class="content">
						<b class="pending" v-if="message.status == 'sending'"></b>
						<b class="send-fail" v-if="message.status == 'fail'"></b>
						<view v-if="message.Datatype == 'text'">{{message.Content}}</view>
						<image class="image-content" v-if="message.Datatype == 'image'" :src="message.Content" :data-url="message.Content" @click="showImageFullScreen" mode="widthFix"></image>
						<view class="video-snapshot"  v-if="message.Datatype == 'video'" :data-url="message.Content" @click="playVideo(message.Content)">
							<image :src="message.payload.thumbnail.url" mode="aspectFit"></image>
							<view class="video-play-icon"></view>
						</view>
						<GoEasyAudioPlayer v-if="message.type =='audio'" :src="message.Content" :duration="5" />
						<!-- <view class="custom-message" v-if="message.type == 'order'">
							<view class="title">
								<image src="../../static/images/dingdan.png"></image>
								<text>自定义消息</text>
							</view>
							<view class="custom-message-item">编号：{{message.payload.number}}</view>
							<view class="custom-message-item">商品: {{message.payload.goods}}</view>
							<view class="custom-message-item">金额: {{message.payload.price}}</view>
						</view> -->
					</view>
				</view>
			</view>
		</view>
		<view class="action-box" v-if="!video.visible">
			<view class="action-top">
				<view :class="[audio.visible ? 'record-icon record-open':'record-icon']" @click="switchAudioKeyboard"></view>
				<view class="record-input" @touchstart="onRecordStart" @touchend="onRecordEnd" v-if="audio.visible" >{{audio.recording ? '松开发送' : '按住录音'}}</view>
				<view class="message-input" v-else>
					<!-- GoEasyIM最大支持3k的文本消息，如需发送长文本，需调整输入框maxlength值 -->
					<input type="text" maxlength="700" placeholder="发送消息" v-model="content" @focus="messageInputFocusin">
				</view>
				<view class="file-icon emoji-icon" @click="showEmoji"></view>
				<view class="file-icon more-icon" @click="showMore"></view>
				<span class="send-message-btn" @click="sendTextMessage">发送</span>
			</view>
			<!--展示表情列表-->
			<!-- <view class="action-bottom" v-if="emoji.show" style="justify-content: space-around">
				<image class="emoji-item" v-for="(emojiItem, emojiKey, index) in emoji.map" :key="index" :src="emoji.url + emojiItem" @click="selectEmoji(emojiKey)"></image>
			</view> -->
			<!--更多-->
			<view class="action-bottom" v-if="more.show">
				<view class="more-item" @click="sendImage">
					<image src="../../static/images/tupian.png"></image>
					<text>图片</text>
				</view>
				<view class="more-item" @click="sendVideo">
					<image src="../../static/images/shipin.png"></image>
					<text>视频</text>
				</view>
				<!-- <view class="more-item" @click="showCustomMessageForm">
					<image src="../../static/images/zidingyi.png"></image>
					<text>自定义消息</text>
				</view> -->
			</view>
		</view>
		<view class="record-loading" v-if="audio.recording"></view>
		<video v-if="video.visible" :src="video.url" id="videoPlayer" @fullscreenchange="onVideoFullScreenChange"></video>
	</view>
</template>

<script>
	import GoEasyAudioPlayer from "../../components/GoEasyAudioPlayer/GoEasyAudioPlayer";
	import EmojiDecoder from "../../js_sdk/EmojiDecoder";
	const recorderManager = uni.getRecorderManager();
	var querydata = {"Cid":0,"Fid":0,"Type":"","Topimg":"","Content":"","Time":"","Other":"",Username:"",Phone:null,Name:"",Datatype:'',Msg:'',State:null}
	export default {
		name: "privateChat",
		components : {
			GoEasyAudioPlayer,
		},
		data() {
			let emojiUrl = 'https://imgcache.qq.com/open/qcloud/tim/assets/emoji/';
			let emojiMap = {
				'[么么哒]': 'emoji_3@2x.png',
				'[乒乓]': 'emoji_4@2x.png',
				'[便便]': 'emoji_5@2x.png',
				'[信封]': 'emoji_6@2x.png',
				'[偷笑]': 'emoji_7@2x.png',
				'[傲慢]': 'emoji_8@2x.png'
			};
			return {
				//聊天文本框
				content: '',
				objdata:{},
				friend: null,
				currentUser: null,
				//已经接收到的消息
				messages: [],
				// msgconfig:{
				// 	pagesize:20,
				// 	page:1
				// },
				//已经加载完所有历史消息
				allHistoryLoaded: false,

				//定义表情列表
				emoji : {
					url : emojiUrl,
					map : emojiMap,
                    show: false,
                    decoder:  new EmojiDecoder(emojiUrl, emojiMap),
				},
				more : {//更多按钮
					show : false
				},
				audio : {
					//语音录音中
					recording : false,
					//录音按钮展示
					visible : false
				},
				video : {
					visible : false,
					url : '',
					context : null
				}
			}
		},
		onReady () {
			this.video.context = uni.createVideoContext('videoPlayer',this);
			// https://uniapp.dcloud.io/api/ui/navigationbar?id=setnavigationbartitle
			uni.setNavigationBarTitle({
				title : 123
			});
		},
		onShow () {
			this.more.show = false;
			this.emoji.show = false;
		},
		onLoad(options) {
			// let imService = getApp().globalData.imService;
			// this.currentUser = uni.getStorageSync('currentUser');
			//聊天对象
            // let friendId = options.to;
			querydata.Cid = this.$userInfo.ID;
			querydata.Fid = Number(options.ID);
			querydata.Phone = this.$userInfo.Phone;
			querydata.Username = this.options.Username;
			querydata.Top = this.$userInfo.Top;
			this.objdata = options;
			// uni.removedata('historylst')
			this.messages = this.loadMoreHistoryMessage(options.ID);
			// console.log(this.messages)
            //从服务器获取最新的好友信息
            // this.friend = imService.findFriendById(friendId);
			// console.log('onLoad friend - ', this.friend);
			// this.messages = imService.getPrivateMessages(friendId);
			//监听新消息
			// imService.onNewPrivateMessageReceive =  (friendId, message)=> {
			// 	if (friendId === this.friend.uuid) {
   //                  //聊天时，收到消息标记为已读
			// 		this.markPrivateMessageAsRead(friendId);
			// 		//收到新消息，是滚动到最底部
			// 		this.scrollToBottom();
			// 	}
			// };
			//每次进入聊天页面，总是滚动到底部
			this.scrollToBottom();
            // 录音监听器
            this.initRecorderListeners();
			//收到的消息设置为已读
			// if(this.messages.length !==0){
			// 	this.markPrivateMessageAsRead(friendId);
			// }
		},
		onPullDownRefresh(e) {
			// this.loadMoreHistoryMessage();
		},
		onUnload() {
			//退出聊天页面之前，清空页面传入的监听器
			var list = uni.getdata("historylst");
			if(!list){
				return
			}
			if(list[querydata.Fid+'key']){
				list[querydata.Fid+'key'].length = 0;
				uni.savedata("historylst",list);
				this.$store.state.$historylst = list;
			}
		},
		watch:{
			$historylst(){
				this.messages = this.loadMoreHistoryMessage(this.objdata.ID);
			}
		},
		onHide() {
		},
		methods: {
            //渲染文本消息，如果包含表情，替换为图片
		    //todo:本不需要该方法，可以在标签里完成，但小程序有兼容性问题，被迫这样实现
            renderTextMessage(message) {
				return '<span class="text-content">' + this.emoji.decoder.decode(message.payload.text) + '</span>'
            },
            //像微信那样显示时间，如果有几分钟没发消息了，才显示时间
            //todo:本不需要该方法，可以在标签里完成，但小程序有兼容性问题，被迫这样实现
            renderMessageDate(message, index) {
                // if (index === 0) {
                //     return this.formatDate(message.timestamp)
                // } else {
                //     if (message.timestamp - this.messages[index - 1].timestamp > 5 * 60 * 1000) {
                //         return this.formatDate(message.timestamp)
                //     }
                // }
				// console.log(message.Time)
                return uni.$u.timeFormat(new Date(message.Time).getTime(), 'yyyy-mm-dd hh:MM:ss');
            },
			initRecorderListeners(){
				// 监听录音开始
				recorderManager.onStart(() => {
					this.audio.recording = true;
				});
				//录音结束后，发送
				recorderManager.onStop((res) => {
					this.audio.recording = false;
					let audioMessage = this.goEasy.im.createAudioMessage({
						to : {
							id : this.friend.uuid,
							type : this.GoEasy.IM_SCENE.PRIVATE,
							data : {
								name:this.friend.name,
								avatar:this.friend.avatar
							}
						},
						file: res,
						onProgress :function (progress) {
							console.log(progress)
						},
						notification : {
							title : this.currentUser.name + '发来一段语音',
							body : '[语音消息]'		// 字段最长 50 字符
						}
					});
					this.sendMessage(audioMessage);
				});
				// 监听录音报错
				recorderManager.onError(function(res){
					console.log("录音报错：",res);
				})
			},
			sendMessage(message){
            	// this.messages.push(message)
				message.Time = new Date();
				this.setHistoryMessage(message)
				uni.$emit("sendSocketMessage",message)
				// this.goEasy.im.sendMessage({
				// 	message: message,
				// 	onSuccess: function (message) {
				// 		console.log("发送成功.", message);
				// 	},
				// 	onFailed: function (error) {
				// 		console.log("发送失败:",error);
				// 	}
				// });
			},
			sendTextMessage() {//发送消息
				if (this.content.trim() !== '') {
					let body = this.content;
					if(this.content.length >= 50){
						body = this.content.substring(0,30)+"...";
					}
					let msgjson = JSON.parse(JSON.stringify(querydata))
					msgjson.Content = this.content.trim();
					msgjson.Datatype = "text";
					this.sendMessage(msgjson);
				}
				this.scrollToBottom();
				this.content = "";
			},
			setHistoryMessage(msg){
				var list = uni.getdata("historylst");
				if(!list){
					list = {}
					list[msg.Fid+'key'] = {name:this.objdata.Name?this.objdata.Name:this.objdata.Username,Cid:msg.Fid,Fid:msg.Cid,list:[msg],Top:this.objdata.Top}
					uni.savedata("historylst",list);
					this.$store.state.$historylst = list;
					return
				}
				if(!list[msg.Fid+'key']){
					list[msg.Fid+'key'] = {name:this.objdata.Name?this.objdata.Name:this.objdata.Username,Cid:msg.Fid,Fid:msg.Cid,list:[msg],Top:this.objdata.Top}
				}else{
					list[msg.Fid+'key'].list.push(msg)
				}
				uni.savedata("historylst",list);
				this.$store.state.$historylst = list;
			},
			loadMoreHistoryMessage(fid) {//历史消息
				let list = uni.getdata("historylst");
				// console.log(list)
				// console.log(list)
				if(!list){
					return [];
				}
				if(!list[fid + 'key']){
					return []
				}
				if(typeof(list[fid + 'key'].list) == "string"){
					return JSON.parse(list[fid + 'key'].list)
				}
				return list[fid + 'key'].list;
			},
			//语音录制按钮和键盘输入的切换
			switchAudioKeyboard() {
				this.audio.visible = !this.audio.visible;
				if(uni.authorize){
					uni.authorize({
						scope : 'scope.record'
					})
				}
			},
			onRecordStart () {
				try{
					recorderManager.start();
				}catch(e){
					uni.showModal({
						title: '录音错误',
						content : '请在app和小程序端体验录音，Uni官方明确H5不支持getRecorderManager, 详情查看Uni官方文档'
					});
				}
			},
			onRecordEnd () {
				try{
					recorderManager.stop();
				}catch(e){
					console.log(e)
				}
			},
			sendVideo () {//发送文件
				uni.chooseVideo({
					success : (res) => {
						let videoMessage = this.goEasy.im.createVideoMessage({
							to : {
								id : this.friend.uuid,
								type : this.GoEasy.IM_SCENE.PRIVATE,
								data : {
									name:this.friend.name,
									avatar:this.friend.avatar
								}
							},
							file: res,
							onProgress :function (progress) {
								console.log(progress)
							},
							notification : {
								title : this.currentUser.name + '发来一个视频',
								body : '[视频消息]'		// 字段最长 50 字符
							}
						});
						this.sendMessage(videoMessage);
					}
				})
			},
			sendImage() {
				uni.chooseImage({
					count :1,
					success :(res) => {
						let imageMessage = this.goEasy.im.createImageMessage({
							to : {
								id : this.friend.uuid,
								type : this.GoEasy.IM_SCENE.PRIVATE,
								data : {
									name:this.friend.name,
									avatar:this.friend.avatar
								}
							},
							file: res,
							onProgress :function (progress) {
								console.log(progress)
							},
							notification : {
								title : this.currentUser.name + '发来一张图片',
								body : '[图片消息]'		// 字段最长 50 字符
							}
						});
						this.sendMessage(imageMessage);
					}
				});
			},
			showImageFullScreen (e) {
				var imagesUrl = [e.currentTarget.dataset.url];
				uni.previewImage({
					urls: imagesUrl
				});
			},
			playVideo (e) {
				this.video.visible = true;
				this.video.url = e;
				this.$nextTick(() => {
					this.video.context.requestFullScreen({
						direction : 0
					});
					this.video.context.play();
				});
			},
			onVideoFullScreenChange (e) {
				//当退出全屏播放时，隐藏播放器
				if(this.video.visible && !e.detail.fullScreen){
				    this.video.visible = false;
				    this.video.context.stop();
				}
			},
			messageInputFocusin () {
				this.more.show = false;
				this.emoji.show = false
			},
			showEmoji () {
				this.emoji.show = !this.emoji.show;
				this.more.show = false;
			},
			showMore () {
				this.more.show = !this.more.show;
				this.emoji.show = false
			},
			selectEmoji (emojiKey) {
				this.content +=emojiKey
			},
			showCustomMessageForm () {
            	let to = {
            		id : this.friend.uuid,
					name : this.friend.name,
					avatar : this.friend.avatar,
					type : this.GoEasy.IM_SCENE.PRIVATE
				};
				uni.navigateTo({
					url: '../customMessage/customMessage?to=' + JSON.stringify(to)
				});
			},
			scrollToBottom () {
				this.$nextTick(function(){
					uni.pageScrollTo({
						scrollTop: 2000000,
						duration : 10
					})
				});
			},
			markPrivateMessageAsRead (friendId) {
				this.goEasy.im.markPrivateMessageAsRead({
					userId: friendId,
					onSuccess: function () {
						console.log('标记为已读成功')
					},
					onFailed: function (error) {
						console.log(error);
					}
				});
			}
		}
	}
</script>

<style >
	
</style>
